package com.rockwell.sniffyhunter.model;

import java.util.ArrayList;

import com.rockwell.sniffyhunter.utils.DetectionIndex;
import com.rockwell.sniffyhunter.utils.IStateMachine;
import com.rockwell.sniffyhunter.utils.UnloaderState;

public class Unloader extends StateObject {

	private UnloadMazeNavigator m_unloadMazeNavigator;
	private MazeWallAligner m_mazeWallAligner;
	private EntranceLocator m_entranceLocator;
	private UnloadMazeLocator m_unloadMazeLocator;
	
	public Unloader(Robot robot) {
		super(robot);
		m_unloadMazeNavigator = new UnloadMazeNavigator(robot);
		m_mazeWallAligner = new MazeWallAligner(robot);
		m_entranceLocator = new EntranceLocator(robot);
		m_unloadMazeLocator = new UnloadMazeLocator(robot);
	}
	
	@Override
	public void Initialize() {
		//m_unloadMazeNavigator.Initialize();
		//m_mazeWallAligner.Initialize();
		//m_entranceLocator.Initialize();
		//m_unloadMazeLocator.Initialize();
		machineState = UnloaderState.START;
	}
	
	@Override
	public boolean IsDone() {
		return machineState == UnloaderState.DONE;
	}

	public IStateMachine LocateMaze() {
		
		ReturnResult returnVal = m_unloadMazeLocator.PerformAction();
		
		if (returnVal == ReturnResult.Completed) {
			log("Unload Maze Located.UnloaderObject", 4);
			machineState = UnloaderState.ALIGN_ROBOT_WITH_MAZE;
		}
		
		return machineState;
	}

	public IStateMachine AlignRobotWithMaze() {
		ReturnResult returnVal = m_mazeWallAligner.PerformAction();
		
		if (returnVal == ReturnResult.Completed) {
			log("AlignRobotWithMaze.UnloaderObject", 4);
			m_entranceLocator.setMaze(m_unloadMazeLocator.getMazeDetected());
			machineState = UnloaderState.LOCATE_ENTRANCE;
		}
		
		return machineState;
	}

	public IStateMachine LocateEntrance() {
		ReturnResult returnVal = m_entranceLocator.PerformAction();
		
		if (returnVal == ReturnResult.Completed) {
			log("LocateEntrance.UnloaderObject", 4);
			machineState = UnloaderState.LOAD_MAZE_TEMPLATE;
		}
		
		return machineState;
	}
	
	public IStateMachine LoadMazeTemplate() {
		String path = "";
		
		int detectedMaze = m_unloadMazeLocator.getMazeDetected();
		switch(detectedMaze)
		{
		case DetectionIndex.MAZE1:
			path = robot.m_easySolver;
			break;
			
		case DetectionIndex.MAZE2:
			path = robot.m_mediumSolver;
			break;
			
		case DetectionIndex.MAZE3:
			path = robot.m_hardSolver;
			break;
		default:
			break;
		}
		
		if (robot.shouldUsePredefined() && path != "") {
			ArrayList<RobotAction> directions = new ArrayList<RobotAction>();
			boolean hasPath = MazePath.parsePathString(path, directions);
			robot.m_solver = new PredefinedMazeSolver(directions, robot.m_solver, robot.m_logger);
		}

		machineState = UnloaderState.UNLOAD;		
		return machineState;
	}

	public IStateMachine Unload() {
		ReturnResult returnVal = m_unloadMazeNavigator.PerformAction();
		
		if (returnVal == ReturnResult.Completed) {
			log("Unload.UnloaderObject", 4);
			machineState = UnloaderState.MAP_MAZE_LOCATION;
		}
		
		return machineState;
	}

	public IStateMachine MapMazeLocation() {
		int returnVal = 1;
		
		if (returnVal == 1 /*go back to sniff*/) {
			machineState = UnloaderState.DONE;
		}
		
		return machineState;
	}
}